package com.zhfi.dataapply.schedule.reader.chaincode;

import com.alibaba.fastjson.JSON;
import com.zhfi.dataapply.entity.TzResponse;
import com.zhfi.dataapply.service.ForwardService;
import com.zhfi.dataapply.service.chaincode.OriginChainCodeListService;
import org.springframework.batch.item.ItemReader;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 根据chainCode和页码pageIndex查询数据
 */
public abstract class AbstractChainPageReader<T> implements ItemReader<T> {


    /**
     * 持久化类型
     */
    private Class<T> clazz;

    /**
     * 企业列表中的偏移量
     */
    private int originChainListSiteNum;

    /**
     * 当前企业数据页码数
     */
    private int pageIndex;

    /**
     * 当前页面中的数据列表
     */
    private List<T> dataList;

    /**
     * 当前页码数据列表中的偏移量
     */
    private int dataSiteNum;


    private List<String> originChainCodeList;


    private ForwardService forwardService;
    private OriginChainCodeListService originChainCodeListService;

    public AbstractChainPageReader(Class<T> clazz, ForwardService forwardService,
                              OriginChainCodeListService originChainCodeListService) {
        this.originChainListSiteNum = 0;
        this.pageIndex = 1;
        this.dataSiteNum = 0;
        this.clazz = clazz;
        this.dataList = Collections.emptyList();
        this.originChainCodeList = Collections.emptyList();
        this.originChainCodeListService = originChainCodeListService;
        this.forwardService = forwardService;
    }

    @Override
    public T read() {
        if (originChainListSiteNum >= originChainCodeList.size()) {
            return null;
        }
        if (CollectionUtils.isEmpty(dataList) || dataSiteNum >= dataList.size()) {
            if (CollectionUtils.isEmpty(dataList) || dataSiteNum < 20) {
                readNextChain();
            } else {
                pageIndex ++;
                dataSiteNum = 0;
                readPage();
            }
            if (CollectionUtils.isEmpty(dataList)) {
                return read();
            }
        }
        return dataList.get(dataSiteNum ++);

    }

    /**
     * 读取当页数据
     */
    private void readPage() {
        //获取当前企业companyId
        String chainCode = originChainCodeList.get(originChainListSiteNum);
        TzResponse tzResponse = forwardService.requestForwarder(getUrl(), getParamsMap(chainCode));
        if (null == tzResponse || CollectionUtils.isEmpty(tzResponse.getData())) {
            readNextChain();
        } else {
            dataList = JSON.parseArray(JSON.toJSONString(tzResponse.getData()), clazz);
        }
    }

    private void readNextChain() {
        originChainListSiteNum ++;
        if (originChainListSiteNum >= originChainCodeList.size()) {
            dataList = Collections.emptyList();
            return;
        }
        pageIndex = 1;
        dataSiteNum = 0;
        dataList = Collections.emptyList();
        //获取当前企业companyId
        String chainCode = originChainCodeList.get(originChainListSiteNum);
        TzResponse tzResponse = forwardService.requestForwarder(getUrl(), getParamsMap(chainCode));
        if (null == tzResponse || CollectionUtils.isEmpty(tzResponse.getData())) {
            readNextChain();
        } else {
            dataList = JSON.parseArray(JSON.toJSONString(tzResponse.getData()), clazz);
        }
    }


    protected Map<String, Object> getParamsMap(String chainCode) {
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("chainCode", chainCode);
        paramsMap.put("pageIndex", pageIndex);
        return paramsMap;
    }

    /**
     * 重置为初始状态
     */
    public void reset() {
        originChainListSiteNum = 0;
        pageIndex = 1;
        dataList = Collections.emptyList();
        originChainCodeList = originChainCodeListService.getOriginChainCodeList();
        dataSiteNum = 0;
        resetUrl();
        readPage();
    }

    public abstract String getUrl();
    protected abstract void resetUrl();

}
